var lastWordStart = 0, n = 0, str = '';
function callback(data) {
    if (data.s.length == 0) return;
    var textarea = get_textarea();
    textarea.value = str.substring(0, lastWordStart) + data.s + ' ';
    textarea.setSelectionRange(n, textarea.value.length);
}
function get_textarea() {
    return document.getElementsByTagName("textarea")[0];
}
function go(event) {
    if (event.keyCode != 13) return;
    event.preventDefault();
    var textarea = get_textarea();
    str = textarea.value;
    n = str.length;
    if (textarea.selectionStart < textarea.selectionEnd) {
        textarea.setSelectionRange(textarea.value.length, textarea.value.length);
        return false;
    }
    if (str[n - 1] == ' ') return false;
    lastWordStart = n - 1;
    while (lastWordStart >= 0 && str[lastWordStart] != ' ') lastWordStart--;
    lastWordStart++;
    $.getJSON("http://94.19.48.125:8089/?firstOnly=1&s=" + encodeURIComponent(str) + "&callback=?", 
        {},
        callback
    );
    return false;
}

